<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="ru">
<head>
    <meta http-equiv="content-type" content="text/html; charset=utf-8" />
    <title>Эмулятор звонка телефона</title>
    <script type="text/javascript" src="js/jquery/jquery.js"></script> 
    <script type="text/javascript" src="js/dbg.js"></script> 
<script>

var boxLog = null;

$(function(){
    boxLog = $("#boxLog");
    
    $("#btnSend").bind("click", check_ring);
    $("#btnSendAsJSON").bind("click", check_ring_json);
    $("#btnSendAsXML").bind("click", check_ring_xml);

    $("#btnRing").bind("click", ring);
    
    $("#aClear").bind("click", function(){boxLog.text(""); return false;});
    
});

function check_ring_json(e)
{
    var phone = $("#phoneHTTP").val();
    var sec = $("#timeout").val();
    
    var req = new Object();
    req.id = "12345";
    req.client_id = "4567";
    req.ring = new Object();
    req.ring.phone = phone;
    req.ring.timeout = sec;

    var sreq = JSON.stringify(req);
    
    var act = "identify2";
    check_ring(e, act, sreq);
}

function check_ring_xml(e)
{
    var phone = $("#phoneHTTP").val();
    var sec = $("#timeout").val();

    var x = '<req id="12345" client_id="3344"><ring phone="'+phone+'" timeout="'+sec+'" /></req>';

    var act = "identify3";
    check_ring(e, act, x);
}

function check_ring(e, act, post_data)
{
    if(act == undefined || act == null) act = "identify";
    var use_get = (post_data == undefined || post_data == null);
    
    log2("act: "+ act);
    var URL = "../common/call.php?act="+act;
    var sdata = "";
    if(use_get)
    {
        URL +=   "&phone=" + $("#phoneHTTP").val();
        URL += "&timeout=" + $("#timeout").val();
        log2("URL: "+ URL);
        split2();
    }
    else
    {
        sdata += "req="+ post_data;
        log2("send: "+ sdata);
        split2();
    }
    
    $.ajax({
        url: URL,
        data: sdata,
        type: use_get ? "GET" : "POST",
        dataType: 'json',
        success: function(data)
        {
            log2(var_dump(data));
            log2(act);
            split2();
        }
    });
}

function send()
{
    var phone = $("#phoneHTTP").val();
    log2(dt() + " register phone: "+ phone);
    request('identify', phone);
}

function ring()
{
    var phone = $("#phoneRing").val();
    log2(dt() + " ring from: "+ phone);
    split2();
    request('call', phone);
}

function request(act, phone)
{
    var sec = $("#timeout").val();
    $.ajax({
        url: "../common/call.php?act="+act+"&timeout="+sec+"&phone="+phone,
        dataType: 'json',
        success: function(data)
        {
            log2(var_dump(data));
            log2(act);
            split2();
        }
    });
}


function two(num)
{
    if(num < 10)
        return '0' + num;
    else
        return num;
}

function dt()
{
    var t = new Date();
    return two(t.getHours()) +":"+ two(t.getMinutes()) +":"+ two(t.getSeconds());
}

function log2(msg)
{
    boxLog.text(msg + "\n" + boxLog.text());
}

function split2()
{
    var s = '-----------------------------------------------------------------';
    log2(s);
}

</script>
</head>

<body style="margin: 20px; font-family: Arial, Verdana;">

    <div style="font-size: 22px;display:block; float: left;">
        <span >Телефон:</span>
        <input id="phoneHTTP"  style="font-size: 22px;" size="12" value="123454321" />
        &nbsp;&nbsp;&nbsp;&nbsp;
        <span >Таймаут:</span>
        <input id="timeout"  style="font-size: 22px; text-align: center;" size="4" value="5" />
        <span >сек</span>
        &nbsp;&nbsp;&nbsp;&nbsp;
        <button id="btnSend" class="large">Test GET</button>
        &nbsp;&nbsp;&nbsp;&nbsp;
        <button id="btnSendAsJSON" class="large">Test JSON</button>
        &nbsp;&nbsp;&nbsp;&nbsp;
        <button id="btnSendAsXML" class="large">Test XML</button>
        <br/><br/>
        <span >Телефон:</span>
        <input id="phoneRing"  style="font-size: 22px;" size="12" value="123454321" />
        <button id="btnRing" class="large">Звонить</button>
        &nbsp;&nbsp;&nbsp;&nbsp;
        <br/><br/>
    </div>
    
    <div style="clear:both;"><a href="#" id="aClear">очистить</a></div>
    
    <pre id="boxLog" style="clear:both;">
    </pre>
</body>
</html>
